import cv2
import numpy as np

filename = 'code_of_teacher/snap_014.jpg'


def get_detector(detector_name):
    if detector_name == 'SIFT':
        return cv2.xfeatures2d.SIFT_create()
    elif detector_name == 'SURF':
        return cv2.xfeatures2d.SURF_create()
    elif detector_name == 'ORB':
        return cv2.ORB_create()
    elif detector_name == 'BRISK':
        return cv2.BRISK_create()





def test():
    img = cv2.imread(filename, cv2.IMREAD_COLOR)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 检测关键点，并提特征取描述子
    detector_name = 'SIFT'
    detector = get_detector(detector_name)
    # 计算关键点  输入图像，输出的是关键点和特征描述子
    kp, des = detector.detectAndCompute(gray, None)
    dst = np.zeros(img.shape, np.uint8)
    # 传入关键点 和原图像 颜色为红色 并绘制 flag 绘制大小、半径、以及主方向
    cv2.drawKeypoints(img, kp, dst, color=(0, 0, 255),
                      flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    cv2.imshow(detector_name + 'Corner', dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


test()
